package com.itbaizhan.clouddemooauth2server.service.impl;


import com.itbaizhan.clouddemooauth2server.domain.LoginUser;
import com.itbaizhan.clouddemooauth2server.service.LoginUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class JdbcUserDetailsService implements UserDetailsService {

    @Autowired
    private LoginUserService loginUserService;

    /**
     * 根据用户名查询用户相关的信息
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //根据页面传递过来的username参数到数据表查询用户信息
        LoginUser loginUser = loginUserService.findByUserName(username);
//        if(loginUser != null){
//            User user = new User(loginUser.getUserName(), loginUser.getPassword(), new ArrayList<>());
//            return user;
//        }

        return  new User(loginUser.getUserName(), loginUser.getPassword(), new ArrayList<>());
    }
}
